﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;

namespace GandA.Web.UI.Controls
{
    [ToolboxData("<{0}:Menu runat=server></{0}:Menu>"),
    ParseChildren(typeof(Item), DefaultProperty = "Items", ChildrenAsProperties = true),
    PersistChildren(false)]
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.Menu))]
    public class Menu : WebControl
    {
        public Menu() {
            _items = new List<Item>();
        }

        #region Propiedades
        private List<Item> _items;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public List<Item> Items
        {
            get { return _items; }
            set { _items = value; }
        }
        
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Ul; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(MenuStyle.Default)]
        [Localizable(true)]
        public MenuStyle StyleMenu
        { 
            get
            {
                return ViewState[this.ID + "StyleMenu"] == null ? (MenuStyle)1 : (MenuStyle)ViewState[this.ID + "StyleMenu"];
            }
            set { ViewState[this.ID + "StyleMenu"] = value; }
        }

        public String FileXml
        {
            get { return ViewState["_fileXml"] == null ? null : (String)ViewState["_fileXml"]; }
            set { ViewState["_fileXml"] = value; }
        }

        public enum MenuStyle
        {
            Default = 1,
            Style2 = 2,
            Style3 = 3,
            Style4 = 4,
            Style5 = 5,
            Style6 = 6,
            Style7 = 7,
            Style8 = 8,
            VStyle1= 9,
            VStyle2 = 10,
            VStyle3 = 11,
            VStyle4 = 12,
            VStyle5 = 13,
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            bool linkIncluded = false;
            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "MenuStyle")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "MenuStyle";
                switch (StyleMenu)
                {
                    case MenuStyle.Style2:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle2.css"));
                        break;
                    case MenuStyle.Style3:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle3.css"));
                        break;
                    case MenuStyle.Style4:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle4.css"));
                        break;
                    case MenuStyle.Style5:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle5.css"));
                        break;
                    case MenuStyle.Style6:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle6.css"));
                        break;
                    case MenuStyle.Style7:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle7.css"));
                        break;
                    case MenuStyle.Style8:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle8.css"));
                        break;
                    case MenuStyle.VStyle1:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle9.css"));
                        break;
                    case MenuStyle.VStyle2:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle10.css"));
                        break;
                    case MenuStyle.VStyle3:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle11.css"));
                        break;
                    case MenuStyle.VStyle4:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle12.css"));
                        break;
                    case MenuStyle.VStyle5:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.MenuStyle13.css"));
                        break;
                    default:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.Menu.css"));
                        break;
                }
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "cssmenu");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.Render(writer);
            writer.RenderEndTag();
        }

        protected override void CreateChildControls()
        {
            if (!string.IsNullOrEmpty(FileXml))
            {
                CargarXml();
            }

            foreach (Item e in Items)
            {
                this.Controls.Add(e);
            }

            base.CreateChildControls();
        } 

        private void CargarXml()
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(FileXml); 
            XmlNodeList menus = xDoc.GetElementsByTagName("menu");

            XmlNodeList lista =
            ((XmlElement)menus[0]).GetElementsByTagName("item");

            int index = 0;
            Items = new List<Item>();
            foreach (XmlElement nodo in lista)
            {
                Items.Add(new Item(nodo.GetAttribute("nombre"), nodo.GetAttribute("url")));
                if (nodo.ChildNodes.Count > 0)
                {
                    Items[index].SubItems = new List<SubItem>();
                    foreach (XmlNode subnodo in nodo.ChildNodes)
                    {
                        SubItem subItem = new SubItem(subnodo.Attributes["nombre"].Value, subnodo.Attributes["url"].Value);
                        foreach (XmlNode subsubnodo in subnodo.ChildNodes)
                        {
                            subItem.SubSubItems.Add(new SubSubItem(subsubnodo.Attributes["nombre"].Value, subsubnodo.Attributes["url"].Value));
                        }

                        Items[index].SubItems.Add(subItem);
                    }
                }

                index++;
            } 
        }

    }

    [ToolboxItem(false)]
    [ToolboxData("<{0}:Item runat=server></{0}:Item>")]
    [ParseChildren(typeof(SubItem), DefaultProperty = "SubItems", ChildrenAsProperties = true),
    PersistChildren(false)]
    public class Item : WebControl
    {
        public Item() {
            _subitems = new List<SubItem>();
        }

        public Item(string description, string url)
        {
            _description = description;
            _url = url;
            _subitems = new List<SubItem>();
        }

        private List<SubItem> _subitems;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public List<SubItem> SubItems
        {
            get { return _subitems; }
            set { _subitems = value; }
        }

        private string _description;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private string _url;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public String Url
        {
            get { return _url; }
            set { _url = value; }
        }
 
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Ul; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, SubItems.Count > 0 ? "has-sub" : "");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            TextWriter innerWrite = writer.InnerWriter;
            innerWrite.Write("<a href=\"" + (string.IsNullOrEmpty(Url) ? "#" : ResolveUrl(Url.Trim())) + "\"><span>" + Description + "</span></a>");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, SubItems.Count > 0 ? "" : "none");
            base.Render(writer);
            writer.RenderEndTag();
        }

        protected override void CreateChildControls()
        {
            if (SubItems.Count > 0)
            {
                //this.Controls.Add(new LiteralControl("<ul>"));
                foreach (SubItem e in SubItems)
                {
                    this.Controls.Add(e);
                }

                base.CreateChildControls();
                //this.Controls.Add(new LiteralControl("</ul>"));
            }
        }
    }

    [ToolboxItem(false)]
    //[ParseChildren(false), PersistChildren(true)]
    [ToolboxData("<{0}:SubItem runat=server></{0}:SubItem>")]
    [ParseChildren(typeof(SubSubItem), DefaultProperty = "SubSubItems", ChildrenAsProperties = true),
    PersistChildren(false)]
    public class SubItem : WebControl
    {
        public SubItem() {
            _items = new List<SubSubItem>();
        }

        public SubItem(string description, string url)
        {
            _description = description;
            _url = url;
            _items = new List<SubSubItem>();
        }

        private List<SubSubItem> _items;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public List<SubSubItem> SubSubItems
        {
            get { return _items; }
            set { _items = value; }
        }

        private string _description;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private string _url;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Ul; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, SubSubItems.Count > 0 ? "has-sub" : "");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, string.IsNullOrEmpty(Url) ? "#" : ResolveUrl(Url.Trim()));
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("<span>" + Description + "</span>");
            writer.RenderEndTag();
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, SubSubItems.Count > 0 ? "" : "none");
            base.Render(writer);
            writer.RenderEndTag();
        }

        protected override void CreateChildControls()
        {
            if (SubSubItems.Count > 0)
            {
                //this.Controls.Add(new LiteralControl("<ul>"));
                foreach (SubSubItem e in SubSubItems)
                {
                    this.Controls.Add(e);
                }

                base.CreateChildControls();
                //this.Controls.Add(new LiteralControl("</ul>"));
            }
        }
    }

    [ToolboxItem(false)]
    [ParseChildren(false), PersistChildren(true)]
    [ToolboxData("<{0}:SubSubItem runat=server></{0}:SubSubItem>")]
    public class SubSubItem : WebControl
    {
        public SubSubItem() {
        }

        public SubSubItem(string description, string url)
        {
            _description = description;
            _url = url;
        }

        private string _description;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private string _url;
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Span; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //writer.AddAttribute(HtmlTextWriterAttribute.Class, "active");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, string.IsNullOrEmpty(Url) ? "#" : ResolveUrl(Url.Trim()));
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("<span>" + Description + "</span>");
            writer.RenderEndTag();
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            base.Render(writer);
            writer.RenderEndTag();
        }
    }
}
